<?php

class ClaimForm extends CFormModel
{
	public $link;
	public $place_id;
	public $owner;
	public $duplicateLink;
	public $claim;
	
	public function rules()
	{
		return array(
			array('place_id, owner, link', 'required'),
			array('link', 'unique', 'attributeName'=> 'link', 'className'=> 'Site', 'message'=> 'Duplicate link'),
			array('link', 'pla2apiv1.extensions.validators.LinkReservedValidator'),
		);
	}
	
	public function claim()
	{
		if(!$this->validate())
		{
			return false;
		}
		$criteria = new CDbCriteria();
		$criteria->condition = "owner = :owner OR place_id = :place_id";
		$criteria->params = array(':owner'=> $this->owner, ':place_id'=> $this->place_id);
		$oldClaim = Claim::model()->find($criteria);
		$oldSite = Site::model()->findByAttributes(array('place_id'=> $this->place_id));
		if($oldClaim == null
			&& $oldSite == null)
		{
			$claim = new Claim();
			$claim->attributes = $this->attributes;
			if($claim->save()){
				return true;
			}
		}
		return false;
	}
}